#include <QPainter>

#include <messaginglayer/cprofile.h>

#include "profileinfowidget.h"

static const int m_profile_pic_size = 48;

ProfileInfoWidget::ProfileInfoWidget(QWidget *parent) :
    QFrame(parent)
{
    setFrameStyle(QFrame::StyledPanel);
}

QSize ProfileInfoWidget::sizeHint() const
{
    QSize size = QWidget::sizeHint();

    size.setHeight(m_profile_pic_size + 10);
    return size;
}

void ProfileInfoWidget::setProfile(CProfile *profile)
{
    Q_ASSERT(profile);

    m_profilePointer = profile;
    m_profileInfo = profile->profileInfo();
    connect(profile, SIGNAL(profileInfoChanged()), this, SLOT(profileInfoChanged()));
}

void ProfileInfoWidget::profileInfoChanged()
{
    CProfile * profile = m_profilePointer.data();
    if (profile)
        m_profileInfo = profile->profileInfo();
}

void ProfileInfoWidget::paintEvent(QPaintEvent *e)
{
    QFrame::paintEvent(e);
    QPainter painter(this);

    QRect picRect(5, 5, m_profile_pic_size, m_profile_pic_size);
    painter.fillRect(picRect.adjusted(-1, -1, 1, 1), Qt::gray);
    painter.fillRect(picRect, Qt::white);
    painter.drawImage(picRect, QImage(":/core/images/default-avatar.png").scaledToHeight(m_profile_pic_size));

    QString name = m_profileInfo.name();
    if (name.isEmpty())
        name = m_profileInfo.id();
    painter.drawText(m_profile_pic_size + 20, 20, name);
}
